package exams.binary;

import java.util.*;

public class TraversalSolution {

    /**
     * 二叉树的前序遍历
     * */
    public int[] preorderTraversal (TreeNode root) {
        // write code here
        List<Integer> list = new ArrayList<>();
        this.preOrder(root, list);
        return list.stream().mapToInt(i -> i).toArray();
    }

    private void preOrder(TreeNode root, List<Integer> list) {
        if(root != null) {
            list.add(root.val);
            preOrder(root.left, list);
            preOrder(root.right, list);
        }
    }

    /**
     * 二叉树的中序遍历
     * */
    public int[] inorderTraversal (TreeNode root) {
        // write code here
        List<Integer> list = new ArrayList<>();
        inorder(root, list);
        return list.stream().mapToInt(i -> i).toArray();
    }

    private void inorder(TreeNode root, List<Integer> list) {
        if (root != null) {
            inorder(root.left, list);
            list.add(root.val);
            inorder(root.right, list);
        }
    }

    /**
     * 二叉树的后序遍历
     * */
    public int[] postorderTraversal (TreeNode root) {
        // write code here
        List<Integer> list = new ArrayList<>();
        this.postOrder(root, list);
        return list.stream().mapToInt(i -> i).toArray();
    }

    private void postOrder(TreeNode root, List<Integer> list) {
        if (root != null) {
            postOrder(root.left, list);
            postOrder(root.right, list);
            list.add(root.val);
        }
    }

    /**
     * 二叉树的层序遍历
     * */
    public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {
        // write code here
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        if(root == null) {
            return res;
        }

        ArrayDeque<TreeNode> queue = new ArrayDeque<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            ArrayList<Integer> lt = new ArrayList<>();
            for (int i = 0; i < size; i ++) {
                TreeNode cur = queue.poll();
                lt.add(cur.val);
                if (cur.left != null) {
                    queue.add(cur.left);
                }
                if (cur.right != null) {
                    queue.add(cur.right);
                }
            }

            res.add(lt);
        }

        return res;
    }

    /**
     * 按之字形顺序打印二叉树
     * */
    public ArrayList<ArrayList<Integer>> Print (TreeNode pRoot) {
        // write code here
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        if (pRoot != null) {
            ArrayDeque<TreeNode> arrDeque = new ArrayDeque<>();
            arrDeque.add(pRoot);
            int dept = 1;
            while (!arrDeque.isEmpty()) {
                ArrayList<Integer> al = new ArrayList<>();
                int size = arrDeque.size();
                for (int i = 0 ; i < size; i++) {
                    TreeNode cur = arrDeque.poll();
                    al.add(cur.val);
                    if (cur.left != null) {
                        arrDeque.add(cur.left);
                    }
                    if (cur.right != null) {
                        arrDeque.add(cur.right);
                    }
                }
                if (dept % 2 == 0) {
                    al = reverseList(al);
                }
                res.add(al);
                dept ++;
            }

        }

        return res;
    }

    private ArrayList<Integer> reverseList(ArrayList<Integer> list) {
        ArrayList<Integer> newlist = new ArrayList<>();
        ListIterator listIterator = list.listIterator(list.size());
        while(listIterator.hasPrevious()) {
            newlist.add((Integer) listIterator.previous());
        }
        return newlist;
    }

    /**
     * 二叉树的最大深度
     * */
    public int maxDepth (TreeNode root) {
        // write code here
        int res = 0;
        if(root == null) {
            return res;
        }
        ArrayDeque<TreeNode> queue = new ArrayDeque<>();
        queue.add(root);
        while(!queue.isEmpty()) {
            int size = queue.size();
            for(int i= 0;i< size; i++) {
                TreeNode cur = queue.poll();
                if(cur.left != null){
                    queue.add(cur.left);
                }
                if(cur.right != null) {
                    queue.add(cur.right);
                }
            }
            res ++ ;
        }

        return res;
    }

    public void dfs(TreeNode root) {
        if(root != null) {
            dfs(root.left);

            dfs(root.right);
            System.out.println(root.val);
        }
    }
}
